package com.springcloud.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONUtil;
import com.springcloud.common.WebSocketConsts;
import com.springcloud.entity.Server;
import com.springcloud.payload.ServerVO;
import com.springcloud.util.ServerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

/**
 * TODO
 *
 * @author 肖嘉亮
 * @version 1.0
 * @date 2021/4/15 16:27
 */

@Slf4j
@Configuration
public class ServerTask {

    @Autowired
    private SimpMessagingTemplate wxTemplate;

    @Scheduled(cron = "0/2 * * * * ?")
    public void websocker() throws  Exception{
        log.info("【正在推送消息】" , DateUtil.formatDateTime(new Date()));
        //查询服务器状态
        Server server = new Server();
        server.copyTo();
        ServerVO serverVO = ServerUtil.wrapServerVO(server);
        Dict dict = ServerUtil.wrapServerDict(serverVO);
        wxTemplate.convertAndSend(WebSocketConsts.PUSH_SERVER , JSONUtil.toJsonStr(dict));
        log.info("【推送消息结束】" , DateUtil.formatDateTime(new Date()));
    }

}
